﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MyRunDataBase;
using BryhtCommon;
using BryhtControls;
using System.IO;
using BryhtShare;

namespace MyRun.Views
{
    public partial class RunResult : PhoneApplicationPage
    {
        public RunResult()
        {
            InitializeComponent();
            GlobalIndicator.Initialize(this);
        }
        private int id = 0;
        private SportModel sport = null;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (NavigationContext.QueryString.ContainsKey("id"))
            {
                id = int.Parse(NavigationContext.QueryString["id"]);
            }

            using (var db = new MyRunDataContent())
            {
                sport = db.Sports.Single(p => p.Id == id);
                var duration = TimeSpan.Zero;
                TimeSpan.TryParse(sport.Duration, out duration);
                txtTimeHour.Text = duration.Hours.ToString();
                txtTimeMinuter.Text = duration.Minutes.ToString();
                txtTimeSecond.Text = duration.Seconds.ToString();
                txtDistance.Text = sport.Distance.ToString("0.00") + "公里";
                txtSpeed.Text = sport.Speed.ToString("0.00") + "公里/小时";
                txtCalorie.Text = sport.Calorie.ToString("0.00") + "Cal";
            }

        }

        //查看路线
        private void ShowMap_Click(object sender, EventArgs e)
        {
            //设置ChinaOffset

            using (var feetDB = new MyRunDataContent())
            {
                if (feetDB.SportFoot.Count(p => p.SportId == id) < 10)
                {
                    ThrowMessage.Show(MessageType.Prompt, "距离太短了，画不出来~~~");
                    return;
                }
                GlobalIndicator.Instance.IsBusy = true;
                GlobalIndicator.Instance.Text = "正在画...";
                var sportFirstFoot = feetDB.SportFoot.Where(p => p.SportId == id).First();
                var soportFirstPoint = new Point(sportFirstFoot.Longitude, sportFirstFoot.Latitude);

                if (sport.ChinaOffsetLattitude == 0 && sport.ChinaOffsetLongtitude == 0)
                {
                    ChinaAddressHelper.GetChinaGooglePosition(soportFirstPoint, (result) =>
                    {
                        if (ChinaAddressHelper.IsGetGeoSuccessful)
                        {
                            using (var db = new MyRunDataContent())
                            {
                                var sp = db.Sports.Single(p => p.Id == id);
                                sp.ChinaOffsetLongtitude = result.X - soportFirstPoint.X;
                                sp.ChinaOffsetLattitude = result.Y - soportFirstPoint.Y;
                                db.SubmitChanges();
                            }
                            this.Dispatcher.BeginInvoke(() =>
                            {
                                NavigationService.Navigate(new Uri("/Views/MapResult.xaml?id=" + id, UriKind.Relative));
                            });
                        }
                        else
                        {
                            ThrowMessage.Show(MessageType.Prompt, "纠偏没有成功，再试一次吧~~~");
                        }
                        GlobalIndicator.Instance.IsBusy = false;
                    });
                }
                else
                {
                    GlobalIndicator.Instance.IsBusy = false;
                    NavigationService.Navigate(new Uri("/Views/MapResult.xaml?id=" + id, UriKind.Relative));
                }
            }

        }

        //删除本条记录
        private void Deleten_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("确定要删除本条记录？", "删除", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                using (var feetDB = new MyRunDataContent())
                {
                    var deleteItem = feetDB.Sports.SingleOrDefault(p => p.Id == id);
                    feetDB.Sports.DeleteOnSubmit(deleteItem);
                    feetDB.SubmitChanges();
                }
                NavigationService.GoBack();
            }

        }


    }
}